package benny_driving_ser.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;
import benny_driving_ser.ObjectBean.UserInfoBean;
import benny_driving_ser.dao.UserInfoDao;
import benny_driving_ser.dao.impl.UserInfoDaoImpl;
import benny_driving_ser.entity.SessionAttributeDriId;
import benny_driving_ser.entity.SessionAttributeUserId;
import benny_driving_ser.util.DBUtil;
import benny_driving_ser.util.JSONUtil;
import benny_driving_ser.util.SessionUtil;

public class UserInfoAction extends BaseAction{
	
	private static final long serialVersionUID = -3894947611847197845L;
	
	private UserInfoDao dao;
	private List<?> list;
	private int userId;
	private PrintWriter out;
	private String userJson;
	private int driId;
	private String pwd;
	
	
	/**
	 * 使用JSONUtil工具类
	 * 
	 * @return
	 */
	public String loadUserInfo(){
		dao = new UserInfoDaoImpl();
		userJson = new String();
		
		SessionAttributeUserId sad = SessionUtil.getInstance()
				.verifySessionIdForUser(request);
		try{
			if(sad!=null){
				userId = Integer.parseInt(sad.getValue());
				list = dao.loadUserInfo(userId);
				request.setCharacterEncoding("utf-8");
				response.setContentType("text/html");
				response.setCharacterEncoding("utf-8");
				out = response.getWriter();
				userJson = JSONUtil.object2json("resforms", list);
				out.println(userJson);
				out.flush();
				out.close();
			}
			out = response.getWriter();
			out.println(-1);
			out.flush();
			out.close();
			
		}catch(IOException e){
			e.printStackTrace();
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		return "success";
	}
	
	
//	private UserInfoDao dao;
//	private UserInfoBean userInfo; 
//	private int userId;
//	private JSONObject userJson;
//	private PrintWriter out;
//	private HashMap<String,Object> json;
//	
//	/**
//	 * 普通方法
//	 * @return
//	 */
//	public String loadUserInfo(){
//		
//		response.setContentType("text/html");
//		dao = new UserInfoDaoImpl();
//		userInfo = dao.loadUserInfo(userId);
////		System.out.println(userInfo.toString());
//		userJson = new JSONObject();
//		
//		response.setCharacterEncoding("utf-8");
//		
//		userInfo = dao.loadUserInfo(userId);
//		try {
//			out = response.getWriter();
//			userJson.accumulate("userInfo", userInfo);
//			out.println(userJson.toString());
//			
//			out.flush();
//			out.close();
//		} catch (JSONException e) {
//			e.printStackTrace();
//		}catch(IOException e){
//			e.printStackTrace();
//		}
//		return "success";
//	}

	
	/**
	 * 修改司机密码
	 */
	public String updateDriPwd() {
		int i = 0;
		dao = new UserInfoDaoImpl();
		
		SessionAttributeDriId sad = SessionUtil.getInstance()
				.verifySessionIdForDri(request);
		try {
			if(sad!=null){
				driId = Integer.parseInt(sad.getValue());
				request.setCharacterEncoding("utf-8");
				response.setContentType("text/html");
				response.setCharacterEncoding("utf-8");
				out = response.getWriter();
				i = dao.updateDriPwd(driId, pwd);
				out.println(i);
				out.flush();
				out.close();
				return "success";
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NumberFormatException e){
			e.printStackTrace();
		}
		
		
		return "fail";
	}
	
	
	public UserInfoDao getDao() {
		return dao;
	}

	public void setDao(UserInfoDao dao) {
		this.dao = dao;
	}

	public List<?> getList() {
		return list;
	}

	public void setList(List<?> list) {
		this.list = list;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public PrintWriter getOut() {
		return out;
	}

	public void setOut(PrintWriter out) {
		this.out = out;
	}

	public String getUserJson() {
		return userJson;
	}

	public void setUserJson(String userJson) {
		this.userJson = userJson;
	}

	public int getDriId() {
		return driId;
	}

	public void setDriId(int driId) {
		this.driId = driId;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	
}
